package com.example.demo.jucConcurrent.ThreadSynchronized;

public class SynchronizedObjectLock02 implements Runnable {
    static SynchronizedObjectLock02 instence = new SynchronizedObjectLock02();
    //创建两把锁
    Object block1 = new Object();
    Object block12 = new Object();


    @Override
    public void run() {
        //这个代码使用的是第一把锁,当他释放后,后面的代码块由于使用的是第二把锁,因此可以马上执行
        synchronized (block1) {
            System.out.println("block1锁线程" + Thread.currentThread().getName());
            //休眠一会
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("block1锁 = " + Thread.currentThread() + "结束");
        }

        synchronized (block12) {
            System.out.println("block12锁线程" + Thread.currentThread().getName());
            //休眠了一会
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("block锁" + Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(instence);
        Thread thread2 = new Thread(instence);
        thread1.start();
        thread2.start();

    }
}
